package com.achievement.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.achievement.dao.CourseRepository;
import com.achievement.dao.SubjectRepository;
import com.achievement.entity.Course;
import com.achievement.entity.Subject;
import com.achievement.entity.dto.StuCourseDTO;
import com.common.util.TablePage;
import com.exam.dao.ExamRepository;
import com.exam.entity.Exam;
import com.user.dao.StudentRepository;
import com.user.dao.TeacherRepository;
import com.user.entity.Student;
import com.user.entity.Teacher;

@Service
@Transactional
public class CourseService implements ICourseService {
	@Autowired
	private CourseRepository courseDao;
	@Autowired
	private SubjectRepository subjectDao;
	@Autowired
	private TeacherRepository teacherDao;
	@Autowired
	private StudentRepository studentDao;
	@Autowired
	private ExamRepository examDao;

	@Override
	public TablePage<Course> findPage(Specification<Course> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		Page<Course> page = courseDao.findAll(spec, pageable);
		return TablePage.getTablePage(page);
	}

	@Override
	public void saveOrUpdate(Course course) {
		// TODO Auto-generated method stub
		/*private Long id;
		private String num;
		private String name;
		private Subject subject;
		private Teacher teacher;*/
/*		Subject subject = null;
		if(course.getSubject()!=null)subject = subjectDao.findOne(course.getSubject().getId());
		Teacher teacher = null;
		if(course.getTeacher()!=null)teacher = teacherDao.findOne(course.getTeacher().getId());
		
		course.setSubject(subject);
		course.setTeacher(teacher);
		*/
		courseDao.save(course);
	}

	@Override
	public List<Course> findAll() {
		// TODO Auto-generated method stub
		return (List<Course>) courseDao.findAll();
	}

	@Override
	public List<Course> findTest() {
		List<Long> ids = new ArrayList<Long>();
		ids.add(7L);
		List<Object> objs = courseDao.findTest(ids);
		Iterator iter = objs.iterator();
		List<Course> courses = new ArrayList<Course>();
		while(iter.hasNext()) {
			Object[] obj = (Object[]) iter.next();
			Course course = (Course) obj[0];
			courses.add(course);
		}
		return courses;
	}
	public List<Course> findUnselect(Long id) {
		List<Course> result = (List<Course>) courseDao.findAll();
		List<Object> objs = courseDao.findByStuId(id);
		Iterator iter = objs.iterator();
		while(iter.hasNext()) {
			Object[] obj = (Object[]) iter.next();
			Course course = (Course) obj[0];
			for(int i=0;i<result.size();i++) {
				if(course.getId()==result.get(i).getId())
					result.remove(i);
			}
		}
		return result;
	}

	public void selectCourse(Long stuId, Long cId) {
		Student student = studentDao.findOne(stuId);
		Course course = courseDao.findOne(cId);
		if(student!=null&&course!=null) {
			course.getStudents().add(student);
			courseDao.save(course);
		}
	}

	public TablePage<StuCourseDTO> findStuDTOPage(Long stuId,Specification<Course> spec, Pageable pageable) {
		Page<Course> page = courseDao.findAll(spec, pageable);
		List<Object> objs = courseDao.findByStuId(stuId);
		List<StuCourseDTO> result = new ArrayList<StuCourseDTO>();
		List<Course> selected = new ArrayList<Course>();
		Iterator iter = objs.iterator();
		while(iter.hasNext()) {
			Object[] obj = (Object[]) iter.next();
			Course course = (Course) obj[0];
			selected.add(course);
		}
		for(Course c:page.getContent()) {
			boolean status = false;
			for(Course s:selected) {
				if(s.getId()==c.getId())
					status = true;
			}
			result.add(new StuCourseDTO(c, status));
		}
		return TablePage.getTablePage(new PageImpl<StuCourseDTO>(result, pageable, page.getTotalElements()));
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		courseDao.delete(id);
	}

	@Override
	public boolean isNumExist(Course course) {
		// TODO Auto-generated method stub
		Course c = courseDao.findByNum(course.getNum());
		if(null!=c&&course.getId()!=c.getId())return true;
		return false;
	}

	@Override
	public boolean hasExam(Long id) {
		// TODO Auto-generated method stub
		if(examDao.findByCourseId(id).size()>0)return true;
		return false;
	}

	@Override
	public boolean hasStudent(Long id) {
		// TODO Auto-generated method stub
		Course course = courseDao.findOne(id);
		if(null!=course&&course.getStudents().size()>0)return true;
		return false;
	}
}
